package com.xiaoxu.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.rental.entity.AutoInfo;
import com.xiaoxu.rental.entity.Violation;
import com.xiaoxu.rental.service.AutoInfoService;
import com.xiaoxu.rental.service.ViolationService;
import com.xiaoxu.rental.util.Result;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiaoxu
 * @since 2024-07-24
 */
@Api(tags = "违章处理" )
@RestController
@RequestMapping("/rental/violation")
@SuppressWarnings("all")
public class ViolationController {

    @Resource
    private ViolationService violationService;

    @Resource
    private AutoInfoService autoInfoService;

    @PostMapping("{start}/{size}")
    public Result search (@PathVariable int start,
                          @PathVariable int size,
                          @RequestBody Violation violation){

        Page<Violation> page = new Page<>(start, size);
        return Result.success(violationService.searchByPage(page,violation));
    }

    @PostMapping
    public Result save(@RequestBody Violation violation){
         AutoInfo autoInfo   =  autoInfoService.selectAutoNum(violation.getAutoNum());
         violation.setAutoId(autoInfo.getId());
        return   violationService.save(violation)?Result.success():Result.fail();
    }

    @PutMapping
    public Result update(@RequestBody Violation violation){
        AutoInfo autoInfo  = autoInfoService.selectAutoNum(violation.getAutoNum());
        violation.setAutoId(autoInfo.getId());
        return violationService.updateById(violation)?Result.success():Result.fail();
    }

    @DeleteMapping("{ids}")
    public Result delete(@PathVariable String ids){
        return  violationService.delete(ids)?Result.success():Result.fail();
    }
}
